/* Copyright (C) Abu Rizal, 2009.
 * 
 * This file is part of QurText.
 * 
 * QurText is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * QurText is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with QurText. If not, see <http://www.gnu.org/licenses/>.
 */
package qurtext.client;

import java.util.ArrayList;
import java.util.TreeMap;

public class MainPresenter {
    private ArrayList<ClientSection> allSections;
	private int currentSection;
	private QurText mainView;
	private UserPresenter userPresenter;
	private SectionPresenter sectionPresenter;
	private NavigationPresenter navigationPresenter;
	private TreeMap<Integer, ClientChapter> allChapterMap;
	
	public NavigationPresenter getNavigationPresenter() {
		return navigationPresenter;
	}

	public void setNavigationPresenter(NavigationPresenter navigationPresenter) {
		this.navigationPresenter = navigationPresenter;
	}

	public UserPresenter getUserPresenter() {
		return userPresenter;
	}

	public void setUserPresenter(UserPresenter userPresenter) {
		this.userPresenter = userPresenter;
	}

	protected void handleError(Throwable caught) {
		if (userPresenter.getCurrentUser().isAdmin) {
			String message = caught.getMessage();
			alert(message);
		}
	}

	public void alert(String message) {
		mainView.alert(message);
	}

	public void setMainView(QurText mainView) {
		this.mainView = mainView;
	}

	public QurText getMainView() {
		return mainView;
	}

	public void setAllSections(ArrayList<ClientSection> allSections) {
		this.allSections = allSections;
	}

	public ArrayList<ClientSection> getAllSections() {
		return allSections;
	}

    public void handleHistoryToken(String historyToken) {
		if (getAllSections()==null) return; //not initialized yet
		if (historyToken.indexOf(":")<0) return;
		int chapter=Integer.parseInt(historyToken.split(":")[0]);
		int verse=Integer.parseInt(historyToken.split(":")[1]);

		sectionPresenter.showSection(findCurrentSection(chapter, verse));
		navigationPresenter.getNavigationView().showCurrentJuz();
	}

	public void setSectionPresenter(SectionPresenter sectionPresenter) {
		this.sectionPresenter = sectionPresenter;
	}

	public SectionPresenter getSectionPresenter() {
		return sectionPresenter;
	}
    
	public ClientSection getCurrentSection() {
		if (null==getAllSections()) return null; //not yet initialized
		return getAllSections().get(currentSection);
	}

	public boolean isProduction() {
		return mainView.isProduction();
	}

	public void redirectUrl(String targetUrl) {
		mainView.redirectUrl(targetUrl);
	}

	public void showSectionPanel() {
		mainView.showSectionPanel();
	}

	public void showJumpPanel() {
		mainView.ShowJumpPanel();
	}

	private ClientSection findCurrentSection(int chapter, int verse) {
		for (int i=0;i<getAllSections().size();i++) {
	    	ClientSection section=getAllSections().get(i);
	    	if (chapter==section.chapter && verse>=section.verse && verse<=section.endVerse) {
	    		currentSection = i;
	    	} 
	    }
	    ClientSection section=getAllSections().get(currentSection);
		return section;
	}

	public String getHistoryToken() {
		return mainView.getHistoryToken();
	}

	public void saveProgressCookie(String readCookie, String lastCookie) {
		mainView.saveProgressCookie(readCookie,lastCookie);
	}

	public void newHistoryItem(String newItem) {
		mainView.newHistoryItem(newItem);
	}

	public String getCurrentUrl() {
		return mainView.getCurrentUrl();
	}

	public void showAdminTab() {
    	navigationPresenter.getNavigationView().showAdminTab();
    	sectionPresenter.getSectionView().showAdminTab();
	}

	public String getCookie(String name) {
		return mainView.getCookie(name);
	}

	public void setAllChapterMap(TreeMap<Integer, ClientChapter> chapterMap) {
		this.allChapterMap = chapterMap;
	}

	public ClientChapter getChapter(int chapterNo) {
		if (null==allChapterMap) return null;
		return allChapterMap.get(chapterNo);
	}
}
